/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model.Servicios;

import Model.Beans.Student;
import Model.Beans.User;
import Model.DAO.UserDAO;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Manuel Lap
 */
public class UserSrv {
    
    private UserDAO userDAO = new UserDAO();
    
     public void addUser(User user){
       userDAO.insertar(user);
   }
    public ArrayList<Student> getStudentsPend(){
        return getStudents((ArrayList<User>)userDAO.searchStudentsPend());
    }
    
    public User getUserByStudent(int idStudent){
        return userDAO.searchUserByStudent(idStudent);
    }
    
    private ArrayList<Student> getStudents(ArrayList<User> users){
        ArrayList<Student> students = new ArrayList<Student>();
        for(int i = 0; i < users.size(); i++){
            ArrayList<Student> studentsTemp = new ArrayList<Student>(users.get(i).getStudents());
            students.add(studentsTemp.get(0));
        }
        return students;
    }
    
    public void updateUser(User user) {
        userDAO.actualizar(user);
    }
    
    public User getUser(String username, String password)
    {
        List<User> users = userDAO.buscar("username='"+username+"'  and  password='"+password+"'", User.class);
        if(!users.isEmpty()){
            return users.get(0);
        }
        
        return null;
    }
}
